import sketch from 'sketch'
import util from './util';

const { saveSlice, imageSourceDir } = util;

const typeMap = {
    image: 0,
    text: 1,
    wrapText: 2,
    circle: 3,
    rect: 4,
    circleImg: 5,
    radiusImg: 6,
    dottleLine: 7,
    radiusRect: 8,
    radiusStroke: 9
};

// 单位转换（x3）
function transformUtil(num) {
    return num * 3;
}

/**
 * 创建图片
 * @param layer
 * @param x
 * @param y
 * @return {{type: number, value: {src: string, w: *, x: *, h: *, y: *}}}
 */
function createImage(layer, x = 0, y = 0) {
    return {
        type: typeMap.image,
        value: {
            src: `$__IMAGE_DIR__$/${imageSourceDir}/${layer.id}@${layer.exportFormats[0].size}.png`,
            x: transformUtil(layer.frame.x + x),
            y: transformUtil(layer.frame.y + y),
            w: transformUtil(layer.frame.width),
            h: transformUtil(layer.frame.height),
        }
    };
}

/**
 * 创建文本
 * @param layer 层
 * @param offsetX x偏移量
 * @param offsetY y偏移量
 * @return {{type: number, value: {fontFamily: string | string, color: (*|string), x: *, y: *, fontSize: string, lineHeight: *, text: *, bold: boolean, align: (*|string)}}}
 * ======================================
 * @todo
 * 1. 需要适配多行createWrapText
 * 2. 需要适配letterSpacing
 * 3. 需要适配文字对齐方式
 */
function createText(layer, offsetX = 0, offsetY = 0) {
    const fontSize = transformUtil(layer.style.fontSize);
    return {
        type: typeMap.text,
        value: {
            text: layer.text,
            fontSize: `${fontSize}px`,
            fontFamily: layer.style.fontFamily || 'Alibaba-PuHuiTi-R',
            align: layer.style.alignment || 'left',
            color: layer.style.textColor || '#000000',
            bold: false,
            x: transformUtil(layer.frame.x + offsetX),
            y: transformUtil(layer.frame.y + offsetY),
            width: transformUtil(layer.frame.width),
            lineHeight: transformUtil(layer.style.lineHeight) || fontSize
        }
    }
}

/**
 * 遍历数据结构
 * @param layers 层组
 * @param savePath 切片保存路径
 * @param { x, y } 父级坐标
 * @return {Array|*}
 */
function traverse(layers, savePath, {
    x = 0,
    y = 0
}) {
    if (!Array.isArray(layers)) {
        return sketch.UI.message('⚠️ 海报数据格式异常');
    }

    const result = [];

    for (const layer of layers) {
        if (layer.exportFormats.length > 0) {
            saveSlice(layer.id, savePath);
            result.push(createImage(layer, x, y));
        }

        if (layer.type.toLowerCase() === 'text') {
            result.push(createText(layer, x, y));
        }

        if (Array.isArray(layer.layers)) {
            result.push(...traverse(layer.layers, savePath, {
                x: x + layer.frame.x,
                y: y + layer.frame.y
            }));
        }
    }

    return result;
}

/**
 * 创建函数
 * @param data 解析数据
 * @param savePath 切片保存路径
 * @return {{data: Array, width: *, height: *}|*}
 */
function parse(data, savePath) {
    if (!data || !data.frame) {
        return sketch.UI.message('⚠️ 海报数据格式异常');
    }

    return {
        width: transformUtil(data.frame.width),
        height: transformUtil(data.frame.height),
        data: traverse(data.layers, savePath, { x: 0, y: 0 })
    }
}

export default {
    parse
}
